package cate.game.role.pvp.eliterace;

import cate.common.table.d.GDEliteRace;
import cate.game.pvp.eliterace.data.EliteRaceKV;
import cate.game.pvp.eliterace.data.EliteRaceRedPacketRecord;
import cate.game.pvp.eliterace.data.EliteRaceSpec;
import cate.game.pvp.eliterace.util.EliteRaceSpecUtils;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.RoleSnapshot;
import cate.game.role.pvp.eliterace.msg.EliteRaceRedPacketResp;
import com.google.common.collect.Sets;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import static cate.game.client.msg.MsgSender.send;

@Data
@NoteClass("精英赛红包数据")
public class EliteRaceRedPacketPO extends RoleDependent {

    @NoteField(value = "已经领取阶段")
    public Set<EliteRaceSpec> gotSet;

    @Override
    public void initialize(Role role) {
        super.initialize(role);

        if (Objects.isNull(gotSet)) {
            gotSet = Sets.newHashSet();
        }
    }


    @Override
    public void onEnter() {
        super.onEnter();
        checkGot();
    }

    /**
     * 跨服合并兼容性处理
     * <p>
     * 在玩家上线和赛季切换的时候进行检查
     */
    private void checkGot() {
        // 大于(当前赛季-1)的所有已经领取的记录需要被清理
        // 当前赛季
        int season = role.getPvp().eliteRace.season;
        gotSet.removeIf(spec -> (season - spec.getSeason()) > 1 || (spec.getSeason() > season));
    }

    public void handleRedPacketReq() {
        if (role.getPvp().eliteRace.betPO.rdMap.size() >= GDEliteRace.MAX_BET_TIMES) {
            role.getGame().notice.message(role, "竞猜次数不足，无法领取红包啦");
            return;
        }
        EliteRaceKV kv = role.getGame().pvp.eliteRace.getDataManager().getData();
        EcResult<List<EliteRaceRedPacketRecord>> r;
        if (!gotSet.contains(kv.getSpec())) {
            r = role.getGame().pvp.eliteRace.getHandler().handleGetRedPacket(new RoleSnapshot(role), kv.getSpec(),role.getGame());
            if (Objects.nonNull(r.data)) {
                List<EliteRaceRedPacketRecord> rdList = r.data;
                EliteRaceRedPacketRecord record = rdList.stream()
                        .filter(rd -> StringUtils.equalsIgnoreCase(rd.getUid(), role.getUid()))
                        .findFirst()
                        .orElse(null);
                if (Objects.nonNull(record)) {
                    gotSet.add(record.getSpec());
                }
            }
        } else {
            r = role.getGame().pvp.eliteRace.getHandler().handleQueryRedPacket(kv.getSpec(), role.getUid());
        }

        if (r.ok()) {
            send(role::sendNow, new EliteRaceRedPacketResp(
                    role.getPvp().eliteRace.redPacketPO,
                    GDEliteRace.FIXED_BET_NUM,
                    r.data
            ));
        }
    }

    public byte hasRedPacket(EliteRaceSpec spec) {
        if (!EliteRaceSpecUtils.isBetRwd(spec)) {
            return 0;
        }

        return !gotSet.contains(spec) ? (byte) 1 : (byte) 0;
    }

    public void onSeasonSwitch() {

    }
}
